using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace GeneralLibs
{
    [Serializable]
    public class LocalSettings
    {
        public static LocalSettings HeThong = LocalSettings.LoadFromFile();

        private static string _ConnectString = "";
        public static bool NeedReloadConnectString = true;
        public static string ConnectString
        {
            get
            {
                if (NeedReloadConnectString)
                {
                    LocalSettings tthh = LocalSettings.LoadFromFile();
                    if (tthh.UseExpertMode)
                    {
                        _ConnectString = tthh.DB_ConnectionString;
                    }
                    else
                    {
                        _ConnectString = string.Format("SERVER = {0}; DATABASE = {1}; UID= {2}; PWD={3}",
                                                            tthh.DB_Server, tthh.DB_Name, tthh.DB_UID, tthh.DB_PASS);
                    }
                    //string dataFile = CommonLib.ThuMucGoc + "\\DB\\Accounting_Data.MDF";
                    //_ConnectString = "Data Source=.\\SQLExpress;Integrated Security=True;User Instance=True;" +
                    //           "AttachDBFilename=" + dataFile;
                    NeedReloadConnectString = false;
                }
                return _ConnectString;
            }
            set
            {
                _ConnectString = value;
            }
        }

        public bool UseExpertMode;
        public string DB_Server = "";
        public string DB_UID = "";
        public string DB_PASS = "";
        public string DB_Name = "";
        public string DB_ConnectionString = "";
        public string TenTruyCap = "";
        public string Password = "";
        public bool GhiLai = false;
        public bool AutoLogin = true;

        public string _NumbericFormatString = "";
        public string NumbericFormatString
        {
            get
            {
                return "### ### ### ### ###";
            }
            set
            {
                _NumbericFormatString = value;
            }
        }

        public CheDoKeToan CheDoKeToan = CheDoKeToan.QD15DoanhNghiepLon;
        public HinhThucKeToan HinhThucKeToan = HinhThucKeToan.NhatKyChung;

        public string SoMay = "";
        public string HardwareID = "";

        public string SerialKey;

        Hashtable _HtData = null;
        public object this[string key]
        {
            get
            {
                if (_HtData == null)
                {
                    _HtData = new Hashtable();
                    return null;
                }
                else
                {
                    if (_HtData.ContainsKey(key)) return _HtData[key];
                    else return null;
                }
            }
            set
            {
                if (_HtData.ContainsKey(key)) _HtData[key] = value;
                else _HtData.Add(key, value);
            }
        }

        public void Save2File()
        {
            try
            {
                // Delete old file
                string fname = CommonLib.ThuMucGoc + "\\Settings.dat";
                if (System.IO.File.Exists(fname)) System.IO.File.Delete(fname);

                System.IO.MemoryStream ms = new MemoryStream();

                // write new information to new file
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, this);
                string encryptedText = CommonLib.EncryptBytes2String(ms.GetBuffer(), "abc");

                StreamWriter sw = new StreamWriter(fname);
                sw.Write(encryptedText);
                sw.Close();

                NeedReloadConnectString = true;
            }
            catch
            {
            }
        }
        public static LocalSettings LoadFromFile()
        {
            LocalSettings LocalSettings = null;
            try
            {
                if (System.IO.File.Exists(CommonLib.ThuMucGoc + "\\Settings.dat"))
                {
                    StreamReader sr = new StreamReader(CommonLib.ThuMucGoc + "\\Settings.dat");
                    string encryptedText = sr.ReadToEnd();
                    sr.Close();
                    byte[] buffer = CommonLib.DecryptString2Bytes(encryptedText, "abc");
                    MemoryStream ms = new MemoryStream(buffer);

                    BinaryFormatter formatter = new BinaryFormatter();
                    object obj = formatter.Deserialize(ms);

                    if (obj == null)
                        LocalSettings = new LocalSettings();
                    else
                    {
                        LocalSettings = (LocalSettings)obj;
                    }
                }
                else LocalSettings = new LocalSettings();
            }
            catch
            {
                LocalSettings = new LocalSettings();
            }
            return LocalSettings;
        }
    }

    public class LocalSettings2 : Hashtable
    {
        public static LocalSettings2 HeThong = LocalSettings2.LoadFromFile();
        public void Save2File()
        {
            try
            {
                // Delete old file
                string fname = CommonLib.ThuMucGoc + "\\Settings.dat";
                if (System.IO.File.Exists(fname)) System.IO.File.Delete(fname);

                System.IO.MemoryStream ms = new MemoryStream();

                // write new information to new file
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, this);
                string encryptedText = CommonLib.EncryptBytes2String(ms.GetBuffer(), "abc");

                StreamWriter sw = new StreamWriter(fname);
                sw.Write(encryptedText);
                sw.Close();

                //NeedReloadConnectString = true;
            }
            catch
            {
            }
        }
        public static LocalSettings2 LoadFromFile()
        {
            LocalSettings2 LocalSettings = null;
            try
            {
                if (System.IO.File.Exists(CommonLib.ThuMucGoc + "\\Settings.dat"))
                {
                    StreamReader sr = new StreamReader(CommonLib.ThuMucGoc + "\\Settings.dat");
                    string encryptedText = sr.ReadToEnd();
                    sr.Close();
                    byte[] buffer = CommonLib.DecryptString2Bytes(encryptedText, "abc");
                    MemoryStream ms = new MemoryStream(buffer);

                    BinaryFormatter formatter = new BinaryFormatter();
                    object obj = formatter.Deserialize(ms);

                    if (obj == null)
                        LocalSettings = new LocalSettings2();
                    else
                    {
                        LocalSettings = (LocalSettings2)obj;
                    }
                }
                else LocalSettings = new LocalSettings2();
                //LocalSettings.NumbericFormatString = "###.###,";
            }
            catch
            {
                LocalSettings = new LocalSettings2();
            }
            return LocalSettings;
        }
    }
}
